package com.shanghaionstar.nav;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.UiSettings;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.map.OMapView;
import com.onstar.cn.map.common.DriveMode;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.iview.INavRoutePlanResultView;
import com.shanghaionstar.nav.presenter.NavRoutePlanResultPresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.MapHandler;
import com.shanghaionstar.weight.TopLayout;

/**
 * @author jun chen
 * @ClassName: NavRoutePlanResultFragment
 * @date 2016年2月18日 上午9:44:53
 * @Description: 路线规划结果页面
 */
public class NavRoutePlanResultFragment extends BaseFragment implements INavRoutePlanResultView {
    private AbstractMapActivity mapActivity;
    //    @FindViewById(R.id.route_plan_result_headview)
//    private HeadViewLayout mHeadView;
    @FindViewById(R.id.mapview)
    private OMapView mapview;
    @FindViewById(R.id.route_time)
    private TextView mTimeTv;
    @FindViewById(R.id.route_space)
    private TextView mSpaceTv;
    private AMap aMap;

    //    @FindViewById(R.id.bottom_detail_view)
//    private LinearLayout bottom_detail_view;
//    @FindViewById(R.id.travel_info_view)
//    private RelativeLayout travel_info_view;
//    @FindViewById(R.id.travel_details)
//    private ImageView travel_details;
    @FindViewById
    private TopLayout top_layout;
    private NavRoutePlanResultPresenter mResultPresenter;

    private MapHandler mapHandler;
    private FragmentManager fragmentManager;
    private UiSettings mUiSettings;

    /**
     * add option for drive mode
     *
     * @param activity
     */
    @FindViewById
    private TextView tv_drivingFastRoad;
    @FindViewById
    private TextView tv_drivingShortDistance;
    @FindViewById
    private TextView tv_drivingSaveMoney;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mapActivity = (AbstractMapActivity) activity;
        fragmentManager = mapActivity.getSupportFragmentManager();
    }

    @Override
    public void onResumeView(View view) {
        super.onResumeView(view);
        mapview.onResume();
    }

    public void initMapSetting() {
        mUiSettings = aMap.getUiSettings();
        //隐藏地图自带的缩放控件
        mUiSettings.setZoomControlsEnabled(false);
        //设置可通过手势缩放
        mUiSettings.setZoomGesturesEnabled(true);
        //指南针
        mUiSettings.setCompassEnabled(false);
    }


    @Override
    public int getLayoutId() {
        return R.layout.nav_route_plan_result;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isFirstLoad()) {
            mapview.onCreate(savedInstanceState);
            aMap = mapview.getMap();
            mapHandler = new MapHandler(mapActivity, mapview.getMap());
            mapActivity.initRouteMapHandler(mapHandler);
            mResultPresenter = new NavRoutePlanResultPresenter(mapActivity.getScheduler(), mapActivity, mapHandler);
            mResultPresenter.setView(this);
            initView();
            Bundle bun = getArguments();
            mResultPresenter.getData(bun);
            initBack();
            initMapSetting();
        }
    }

    private void initBack() {
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.Routeresults_return.getStringValue());
                goBack();
            }
        });
    }


    private TextView[] modeTextViews;

    @OnClick({R.id.travel_details, R.id.tv_drivingFastRoad, R.id.tv_drivingShortDistance, R.id.tv_drivingSaveMoney})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.travel_details://详情页面
                BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Routeresults_detailclose.getStringValue());
                mResultPresenter.detailClick();
                break;
            case R.id.tv_drivingFastRoad:
                changeColor(tv_drivingFastRoad);
                mResultPresenter.getDriveModeResult(DriveMode.DrivingFastRoad);
                break;
            case R.id.tv_drivingShortDistance:
                changeColor(tv_drivingShortDistance);
                mResultPresenter.getDriveModeResult(DriveMode.DrivingShortDistance);
                break;
            case R.id.tv_drivingSaveMoney:
                changeColor(tv_drivingSaveMoney);
                mResultPresenter.getDriveModeResult(DriveMode.DrivingSaveMoney);
                break;
        }
    }

    private void changeColor(TextView currentView) {
        currentView.setTextColor(getResources().getColor(R.color.nav_font_blue));
        for (TextView textView : modeTextViews) {
            if(currentView != textView) {
                textView.setTextColor(getResources().getColor(R.color.nav_font_color));
            }
        }
    }


    public void initView() {
         modeTextViews = new TextView[]{tv_drivingFastRoad, tv_drivingShortDistance, tv_drivingSaveMoney};
//        mHeadView.setLeftBtnOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                BusinessUtil.sendReportByClickType(mActivity,
//                        FunctionNameFindId.Routeresults_return.getStringValue());
//                back();
//            }
//        });
    }


    @Override
    public void onResume() {
        super.onResume();
        mResultPresenter.onResume(mapview);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapview.onSaveInstanceState(outState);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mapview != null) {
            mapview.removeAllViews();
            mapview.onDestroy();
            mapview = null;
        }
        mResultPresenter.onDestroy(mapview);
    }

    @Override
    public void onStop() {
        super.onStop();
        mResultPresenter.onStop();
    }


    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(Object object) {

    }

    @Override
    public Activity onActivity() {
        return null;
    }

    @Override
    public void setTxt(String space, String time) {
        mSpaceTv.setText(space);
        mTimeTv.setText(time);
    }


    public static NavRoutePlanResultFragment newInstance(PoiItemInfo routeStartPoiItemInfo, PoiItemInfo routeEndPoiItemInfo) {
        NavRoutePlanResultFragment navRoutePlanResultFragment = new NavRoutePlanResultFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("routeStartPoiItemInfo", routeStartPoiItemInfo);
        bundle.putSerializable("routeEndPoiItemInfo", routeEndPoiItemInfo);
        navRoutePlanResultFragment.setArguments(bundle);
        return navRoutePlanResultFragment;
    }
}
